jQuery(document).ready(function() { 
    /*
	ua_tab2ics = "IMM76D";
	_ua = navigator.userAgent;
	if(_ua.search(ua_tab2ics) > 0){
	//do nothing
	}
	else{
	//Set the expiration of cookies to 30 mins.
    var d = new Date();
	var expiration = getCookie("tiangge_tiangge_expiration");
	if(expiration=="")
		setCookie("tiangge_expiration",d.getTime()); 
	} */

	/*Welcome Page*/
	//resizeIframe();

	jQuery(window).on("resize", function(event){
		//console.log("1" );
		resizeIframe();
		/*
		if ("window", $( window ).width() == 1024) {
		console.log("2" ); 
		resizeIframe();
		}*/
	});

	/* if( /Android|iPhone|Opera Mini/i.test(navigator.userAgent)){ 
	jQuery("#plancodebutton").insertAfter( "#errorForRef" );
	jQuery(".blueLine").css("margin-top","60px");
	} */
	
    jQuery('#inputplancode').bind("cut copy paste",function(e) {
     	 e.preventDefault();
  	});	
	
	jQuery('#inputplancode').keypress(function (e) {
	 var key = e.which;
	 if(key == 13)  // the enter key code
	  {
		jQuery("#plancodebutton").click();
	  }
	});
	
	if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
		jQuery(".buttonBlue.Wel ").css("padding","5px 41px 0px 41px");
	}

	_ua = navigator.userAgent;
	/* FIX FOR FUCKING TAB 2 BLACK */
	if(_ua.search("IMM76D") > 0) {
		//alert("tab 2 black"); 
		jQuery("#registered").click(function() {
			jQuery("#errorForRef").hide();	
			jQuery("#inputplancode").val("");
			jQuery("#inputplancode").removeClass("error");
			jQuery('.dialog_overlay').fadeIn();
			jQuery('#modalmobtel').fadeIn();
			if ("window", $( window ).width() == 600) {
				jQuery(".dialog_overlay").css({
					"max-height":"1000%",
					"height":"1000%"
				});	
				jQuery(".modalStyle h1").css("font-size","17px");
				jQuery('#modalmobtel').css({"z-index":"9999","position":"absolute","top":"29%","width":"100%"});
				yPos = jQuery("#modalmobtel").position().top;
				yPos = parseInt(yPos);
				scrollFrameHeight(yPos);
				//alert("600 " +yPos);
			} else if ("window", $( window ).width() == 1024) {
				jQuery(".dialog_overlay").css({
					"max-height":"1000%",
					"height":"1000%"
				});	
				jQuery('#modalmobtel').css({"z-index":"9999","position":"absolute","top":"8%","width":"100%"});	
				scrollFrameHeight('200'); 
				//alert("1024 200");
			}
		});
	/* FIX FOR FUCKING TAB 2 WHITE */
	} else if (_ua.search("GT-P31") > 0) {
		//alert("tab 2 white");
		jQuery("#registered").click(function() {
			jQuery("#errorForRef").hide();	
			jQuery("#inputplancode").val("");
			jQuery("#inputplancode").removeClass("error");
			jQuery('.dialog_overlay').fadeIn();
			jQuery('#modalmobtel').fadeIn();
			if ("window", $( window ).width() == 600) {
				jQuery(".dialog_overlay").css({
					"max-height":"1000%",
					"height":"1000%"
				});	
				jQuery(".modalStyle h1").css("font-size","17px");
				jQuery('#modalmobtel').css({"z-index":"9999","position":"absolute","top":"29%","width":"100%"});
				yPos = jQuery("#modalmobtel").position().top;
				yPos = parseInt(yPos);
				scrollFrameHeight(yPos); 
				//alert("600 "+yPos+"");
			} 
			if ("window", $( window ).width() == 1024) {
				jQuery(".dialog_overlay").css({
					"max-height":"1000%",
					"height":"1000%"
				});	
				jQuery('#modalmobtel').css({"z-index":"9999","position":"absolute","top":"8%","width":"100%"});	
				scrollFrameHeight('200'); 
				//alert("1024");
			} 
		}); 
	} else {
		jQuery("#registered").click(function() {
			showRetrieveBox();
		});
	}
	
	jQuery(".closeIcon").click(function() {
		//jQuery("#modal").attr("style","display:none!important;");	
		jQuery('.dialog_overlay').fadeOut();
		jQuery('#modalmobtel').fadeOut();
		//return false;
	});
	
	jQuery("#btn_create_gadget").click(function() {
		var current_pos = getCookie("tiangge_current_pos");
		if (current_pos == "") {
			setCookie("tiangge_current_pos","1");
		}	
		//document.cookie = 'gadgetid=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';	
	});
	
	jQuery("#plancodebutton").click(function() {
		var inputplancode = jQuery("#inputplancode").val();
		
		if(inputplancode=="") {
			jQuery("#inputplancode").addClass("error");
			jQuery("#errorForRef").show();
			return false;
		} else {
			jQuery.getJSON(baseurl + "welcome/enterPlancode/" + inputplancode,{ajax: 'true'}, function(j){
				if(j.status==0) {
					jQuery("#inputplancode").removeClass("error");
					//redirect_url = j.redirect;
					//redirect_parsed = redirect_url.replace(/\\/g, "");
					redirect_parsed = j.redirect.replace(/\\/g, "");
					//console.log(redirect_parsed);
					window.location=redirect_parsed+"/index/"+inputplancode;
					//document.location.url=j.redirect;
					jQuery("#errorForRef").hide();
				} else {
					jQuery("#inputplancode").addClass("error");
					jQuery("#errorForRef").show();
					return false;
				}	
			});
		}
	});
	
	isRetrieve();
	/*End Welcome Page*/

});

function showRetrieveBox() {
	jQuery("#errorForRef").hide();	
	jQuery("#inputplancode").val("");
	jQuery("#inputplancode").removeClass("error");
	jQuery('.dialog_overlay').fadeIn();
	jQuery('#modalmobtel').fadeIn();
	if ("window", $( window ).width() < 641) {	
		jQuery(".modalStyle h1").css("font-size","17px");	
		jQuery('#modalmobtel').css({"z-index":"9999","position":"absolute","top":"29%","width":"100%"});
		yPos = jQuery("#modalmobtel").position().top;
		yPos = parseInt(yPos);
		scrollFrameHeight(yPos);	
	} else if ("window", $( window ).width() == 768) {
		jQuery(".modalStyle h1").css("font-size","17px");	
		jQuery('#modalmobtel').css({"z-index":"9999","position":"absolute","top":"15%","width":"100%"});
		scrollFrameHeight('200');
	} else {
		jQuery('#modalmobtel').css({"z-index":"9999","position":"absolute","top":"300px","width":"100%"});	
		scrollFrameHeight('200'); 	
	}
}

function isRetrieve() {
	if(location.href.match(/(\?|&)retrieveplan($|&|=)/)) {
		showRetrieveBox();
	}
}
